<?php

namespace App\Models\Purchase;

use App\Models\Custom\Custom;
use App\Models\Manger\Manger;
use App\Models\Settle\SettleCompany;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;

class PurchaseAgreement extends Model
{
    protected $table = 'purchase_agreement';
    public $timestamps = false;
    protected $primaryKey = 'id';
    protected $guarded = ['_token'];

    public static function getList(Request $request, $cpid, $manger_id = 0, $type = 0, $isgetall = 0)
    {

        $query = self::with([
            'mangerinfo' => function ($query) {
                $query->select(['id', 'realname', 'username']);
            },
            'custominfo' => function ($query) {
                $query->select(['id', 'company_name']);
            },
            'settlecompany' => function ($query) {
                $query->select(['id', 'settle_no', 'settle_company_name']);
            },
            'pinfo' => function ($query) {
                $query->select(['id', 'contract_no as ptitle']);
            },

        ])->where(['cpid' => $cpid])->orderBy('create_at', 'desc');

        if (in_array($type, [1, 2, 3,4])) {
            $query->where('type', $type);
        }
        $sn_code = $request->sn_code;
        if ($sn_code != '') {
            $query->where('sn_code', 'like', '%' . $sn_code . '%');
        }

        $sales_manger_name=$request->sales_manger_name;
        if($type==4 && $sales_manger_name!=''){
            $query->whereHas('mangerinfo',function($q) use ($sales_manger_name){
                $q->where('realname','like','%'.$sales_manger_name.'%');
            });
        }



        // $purchase_type= $request->purchase_type ?? 0;

        // if (in_array($purchase_type, [1, 2])) {
        //     $query->where('purchase_type', $purchase_type);
        // }
        if ($manger_id > 0) {
            $query->where('manger_id', $manger_id);
        }
        $contract_no = $request->contract_no;
        if ($contract_no != '') {
            $query->where('contract_no', 'like', '%' . $contract_no . '%');
        }

        $custom_id = $request->custom_id;
        if (intval($custom_id) > 0) {
            $query->where('custom_id', $custom_id);
        }


        $settle_company_name = $request->settle_company_name;

        if ($settle_company_name != '') {
            $settle_company_id_arr = SettleCompany::where('settle_company_name', 'like', '%' . $settle_company_name . '%')->pluck('id');
            $query->whereIn('settle_company_id', $settle_company_id_arr);
        }


        $company = $request->company;

        if ($company != '') {
            $custom_id_arr = Custom::where('company_name', 'like', '%' . $company . '%')->pluck('id');
            $query->whereIn('custom_id', $custom_id_arr);
        }

        $start_time = isTimestamp($request->start_time) ? $request->start_time : strtotime($request->start_time);
        $end_time = isTimestamp($request->end_time) ? $request->end_time : strtotime($request->end_time);
        if ($start_time > 0 && $end_time > 0) {
            $query->where('contract_date', '>=', $start_time)->where('contract_date', '<=', $end_time);
        }
        if ($isgetall == 0) {
            $limit = $request->limit ?? 10;
            $list = $query->paginate($limit)->toArray();
            return paginateDataToArray($list);
        } else {
            $list = $query->get()->toArray();

            return ['list' => $list];
        }
    }

    public function pinfo()
    {
        return $this->hasOne(PurchaseAgreement::class, 'id', 'pid');
    }

    public function mangerinfo()
    {
        return $this->hasOne(Manger::class, 'id', 'sales_manger_id');
    }

    public function custominfo()
    {
        return $this->hasOne(Custom::class, 'id', 'custom_id');
    }
    public function settlecompany()
    {
        return $this->hasOne(SettleCompany::class, 'id', 'settle_company_id');
    }


    public function getWeightAttribute($value)
    {
        return $value / 10;
    }
}
